<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-14
 * Time: 18:31
 */

namespace app\api\service\partner;


use app\api\model\Assistant;
use app\api\model\CityPartner;
use app\api\model\CountyPartner;
use app\api\model\Member;
use app\api\model\ProvincePartner;
use app\api\model\User;
use app\api\service\Token;
use app\lib\exception\ParameterException;

class CreateAssistant extends Token
{
    // 邀请人ID
    protected $inviteUserId;

    // 邀请人类型
    protected $status;

    // 邀请人信息
    protected $invite;

    // 当前用户的会员信息
    protected $member;

    function __construct($inviteUserId)
    {
        $this->inviteUserId = $inviteUserId;
    }

    /**
     * 判断用户类型  省，市，区
     * 查询信息并判断该信息下的助理数量
     * 创建成功
     */
    public function set()
    {
        self::judgeMember();
        $uid = self::getCurrentUid();
        $this->member = Member::where('user_id',$uid)->find();
        $inviteUser = User::where('id',$this->inviteUserId)->find();
        if($inviteUser['partner_status'] == 0 || $inviteUser['partner_status'] == 4) {
            throw new ParameterException([
               'msg' => [
                   'assistant' => '邀请人还不是合伙人'
               ]
            ]);
        }
        $this->status = $inviteUser['partner_status'];
        return $this->judge();
    }

    // 判断助理数量
    private function judge()
    {
        if($this->status == 1) {
            // 省级
            $this->invite = ProvincePartner::where('user_id',$this->inviteUserId)->find();
            if(!$this->invite) {
                throw new ParameterException([
                    'msg' => [
                        'assistant' => '请重新扫码'
                    ]
                ]);
            }
        } elseif($this->status == 2) {
            // 市级
            $this->invite = CityPartner::where('user_id',$this->inviteUserId)->find();
            if(! $this->invite) {
                throw new ParameterException([
                    'msg' => [
                        'assistant' => '请重新扫码'
                    ]
                ]);
            }
        } elseif($this->status == 3) {
            // 区县
            $this->invite = CountyPartner::where('user_id',$this->inviteUserId)->find();
            if(! $this->invite) {
                throw new ParameterException([
                    'msg' => [
                        'assistant' => '请重新扫码'
                    ]
                ]);
            }
        }
        $number = Assistant::where('type',$this->status)->where('pid',$this->invite['id'])->count();
        if($number >= $this->invite['assistant_number']) {
            throw new ParameterException([
                'msg' => [
                    'assistant' => '助理数已达上线'
                ]
            ]);
        }
        $provincePartner = ProvincePartner::where('user_id',$this->member['user_id'])->find();
        if($provincePartner) {
            throw new ParameterException([
                'msg' => [
                    'partner' => '您已经是省级合伙人'
                ]
            ]);
        }
        $cityPartner = CityPartner::where('user_id',$this->member['user_id'])->find();
        if($cityPartner) {
            throw new ParameterException([
                'msg' => [
                    'partner' => '您已经是市级合伙人'
                ]
            ]);
        }
        $countPartner = CountyPartner::where('user_id',$this->member['user_id'])->find();
        if($countPartner) {
            throw new ParameterException([
                'msg' => [
                    'partner' => '您已经是区县合伙人'
                ]
            ]);
        }
        $assistant = Assistant::where('user_id',$this->member['user_id'])->find();
        if($assistant) {
            throw new ParameterException([
                'msg' => [
                    'partner' => '您已经是助理'
                ]
            ]);
        }
        return $this->createData();
    }

    // 创建信息
    private function createData()
    {
        Assistant::create([
            'name' => $this->member['name'],
            'phone' => $this->member['phone'],
            'id_card' => $this->member['id_card'],
            'user_id' => $this->member['user_id'],
            'pid' => $this->invite['id'],
            'type' => $this->status,
        ]);
        User::where('id',$this->uid)->update([
            'partner_status' => 4
        ]);
        return writeJson(201, [], '绑定成功！');
    }
}